package com.ssc.blonvia.opencra.intro;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.jface.viewers.ArrayContentProvider;
import org.eclipse.jface.viewers.CellEditor;
import org.eclipse.jface.viewers.CheckboxCellEditor;
import org.eclipse.jface.viewers.ColumnLabelProvider;
import org.eclipse.jface.viewers.ColumnWeightData;
import org.eclipse.jface.viewers.EditingSupport;
import org.eclipse.jface.viewers.ICellModifier;
import org.eclipse.jface.viewers.TableLayout;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.jface.viewers.TableViewerColumn;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.forms.widgets.Form;
import org.eclipse.ui.forms.widgets.FormToolkit;
import org.eclipse.ui.forms.widgets.TableWrapData;
import org.eclipse.ui.forms.widgets.TableWrapLayout;
import org.eclipse.ui.part.ViewPart;

import com.ssc.blonvia.opencra.Activator;

public class MainView extends ViewPart {
	public static final String ID = "OpenCRAMain.view";
	Display display = PlatformUI.getWorkbench().getDisplay();
	// Text fichierDonnees;
	String fileFilterPath = System.getProperty("user.dir");
	private static final Image CHECKED = Activator.getImageDescriptor(
			"icons/checked.gif").createImage();
	private static final Image UNCHECKED = Activator.getImageDescriptor(
			"icons/unchecked.gif").createImage();
	FormToolkit toolkit;
	Form form;
	TableWrapData gridData;
	String[] titles;
	MONTHS currentMonth;
	TableViewer tableViewer;

	static class CraItem {
		private String name;

		List<Boolean> jourTravaille;
		int nbJoursTotal;

		public CraItem(int nombreJourtotal, String _name) {
			nbJoursTotal = nombreJourtotal;
			this.name = _name;
		}

		public List<Boolean> getJourTravaille() {
			if (jourTravaille == null) {
				jourTravaille = new ArrayList<Boolean>(nbJoursTotal);
				for (int i=0;i<nbJoursTotal;i++) {
					jourTravaille.add( Boolean.FALSE);
				}
			}
			return jourTravaille;
		}

		public String getName() {
			return name;
		}

	}
	class FirstNameEditingSupport extends EditingSupport {

		private final TableViewer viewer;
		int col;
		public FirstNameEditingSupport(TableViewer viewer, int col) {
			super(viewer);
			this.viewer = viewer;
			this.col = col;
		}

		@Override
		protected CellEditor getCellEditor(Object element) {
			
			return new CheckboxCellEditor(viewer.getTable());
		}

		@Override
		protected boolean canEdit(Object element) {
			return true;
		}

		@Override
		protected Object getValue(Object element) {
			
			return ((CraItem) element).getJourTravaille().get(col);
		}

		@Override
		protected void setValue(Object element, Object value) {
			((CraItem) element).getJourTravaille().set(col, ((Boolean)value));
			System.out.println(element + "- new Val : " + value);
;			viewer.refresh();
		}
	}

	/**
	 * This is a callback that will allow us to create the viewer and initialize
	 * it.
	 */
	public void createPartControl(final Composite parent) {

		toolkit = new FormToolkit(parent.getDisplay());
		form = toolkit.createForm(parent);
		GridLayout layout = new GridLayout(2, false);
		
		form.getBody().setLayout(layout);
		tableViewer = new TableViewer(form.getBody(), SWT.BORDER
				| SWT.FULL_SELECTION);
		toolkit.adapt(tableViewer.getTable(), false, false);

		// tableViewer.setc
		ArrayList<String> colPros = new ArrayList<String>();
		colPros.add("Utilisateur");
		currentMonth = MONTHS.getCurrentMonth();
		for (int i = 1; i <= currentMonth.getMaxDayInMonth(); i++) {
			colPros.add(String.valueOf(i));
		}
		final CraItem item1 = new CraItem(currentMonth.getMaxDayInMonth(),
				"Emmanuel");
		final CraItem item2 = new CraItem(currentMonth.getMaxDayInMonth(),
				"Marc");
		titles = colPros.toArray(new String[] {});
		createColumns(form.getBody(), tableViewer);
		tableViewer.setContentProvider(new ArrayContentProvider());
		tableViewer.setInput(new CraItem[] { item1, item2 });
		tableViewer.getTable().setHeaderVisible(true);
		tableViewer.getTable().setLinesVisible(true);
		getSite().setSelectionProvider(tableViewer);
		// Set the sorter for the table

		// Layout the viewer
		GridData gridData = new GridData();
		gridData.verticalAlignment = GridData.FILL;
		gridData.horizontalSpan = 2;
		gridData.grabExcessHorizontalSpace = true;
		gridData.grabExcessVerticalSpace = true;
		gridData.horizontalAlignment = GridData.FILL;
		tableViewer.getControl().setLayoutData(gridData);
		
		form.setText("Annuaire Shalom Tabernacle");

	}

	// This will create the columns for the table
	private void createColumns(final Composite parent, final TableViewer viewer) {
	

		// First column is for the first name
		for (int i = 0; i < currentMonth.getMaxDayInMonth(); i++) {
			final int i2 = i;
			TableViewerColumn col = createTableViewerColumn(titles[i], i);
			col.setLabelProvider(new ColumnLabelProvider() {
				@Override
				public String getText(Object element) {
					CraItem p = (CraItem) element;
					if (i2 == 0) {

						return p.getName();
					}
					return "";
				}

				@Override
				public Image getImage(Object element) {
					if(i2==0)return null;
					Boolean b = ((CraItem) element).getJourTravaille().get(i2);
					if (b) {
						return CHECKED;
					} else {
						return UNCHECKED;
					}
				}
			});
			if(i2!=0)col.setEditingSupport(new FirstNameEditingSupport(viewer, i));
			else {}
		}

		// // Now the status married

	}

	private TableViewerColumn createTableViewerColumn(String title,
			final int colNumber) {
		final TableViewerColumn viewerColumn = new TableViewerColumn(
				tableViewer, SWT.NONE);
		final TableColumn column = viewerColumn.getColumn();
		column.setText(title);
		if(colNumber==0)column.setWidth(100);
		else column.setWidth(20);
		column.setResizable(true);
		column.setMoveable(true);
		return viewerColumn;
	}

	/**
	 * Passing the focus request to the viewer's control.
	 */
	public void setFocus() {
		// fichierDonnees.setFocus();
	}
}